package cn.liangxxhu.gateway.mq.consumer;

import cn.liangxxhu.gateway.mq.constant.MessageConstant;
import cn.liangxxhu.rocketmq.core.FailedMessageDao;
import cn.liangxxhu.rocketmq.core.param.MessageWrapper;
import cn.liangxxhu.gateway.mq.domain.PayResultCallbackOrderEvent;
import cn.liangxxhu.rocketmq.core.AbstractCommonConsumerTemplate;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 订单取消消息消费者。
 * 使用 @RocketMQMessageListener 订阅指定 topic/tag，并将消费委托给抽象模板处理。
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = MessageConstant.OrderMessageConstant.TOPIC,
        selectorExpression = MessageConstant.OrderMessageConstant.TAG,
        consumerGroup = MessageConstant.OrderMessageConstant.GROUP,
        consumeThreadNumber = MessageConstant.THREAD_NUMBER,
        maxReconsumeTimes = MessageConstant.MAX_RECONSUME_TIMES,
        consumeTimeout = MessageConstant.CONSUME_TIMEOUT,
        delayLevelWhenNextConsume = MessageConstant.DELAY_LEVEL_WHEN_NEXT_CONSUME
)
public class OrderCancelConsumer extends AbstractCommonConsumerTemplate<PayResultCallbackOrderEvent>
        implements RocketMQListener<MessageExt> {

    public OrderCancelConsumer(FailedMessageDao failedMessageDao) {
        super(failedMessageDao);
    }

    @Override
    public void onMessage(MessageExt message) {
        dispatchMessage(buildWrapperFromMessageExt(message));
    }

    @Override
    protected void handleMessage(MessageWrapper<PayResultCallbackOrderEvent> message) {
        log.info("处理消息超时取消：{}", JSON.toJSONString(message));
        // 在此处编写实际的业务处理逻辑
    }

    @Override
    protected TypeReference<MessageWrapper<PayResultCallbackOrderEvent>> getMessageWrapperType() {
        return new TypeReference<MessageWrapper<PayResultCallbackOrderEvent>>() {};
    }
}
